package com.peng.leetcode.backtracking;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * Subsets
 *
 * @author: lupeng6
 * @create: 2020/12/28 20:53
 */
public class Subsets {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        System.out.println(subsets(nums));
    }

    public static List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length <= 0) {
            return result;
        }
        Deque<Integer> path = new ArrayDeque<>();
        dfs(nums, 0, path, result);
        return result;
    }

    public static void dfs(int[] nums, int current, Deque<Integer> path, List<List<Integer>> result) {
        result.add(new ArrayList<>(path));
        for (int i = current; i < nums.length; i++) {
            path.offer(nums[i]);
            dfs(nums, i + 1, path, result);
            path.removeLast();
        }
    }
}
